package com.lucian.ge.controller.shopportal;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lucian.ge.auth.controller.GetAuthController;
import com.lucian.ge.service.shop.CartService;
import com.lucian.ge.service.shop.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lucian.ge.comman.model.ResultMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lucicol on 2019/6/24.
 */
@Api(value = "CartController", description = "cart-购物车接口")

@RestController
@RequestMapping("/cart")
public class CartController {
    private final ResultMap resultMap;

    @Autowired
    public CartController(ResultMap resultMap) {
        this.resultMap = resultMap;
    }

    @Autowired(required=false)
    private CartService cartService;

    @Autowired(required=false)
    private ProductService productService;

    /**
     * 添加购物车
     */
    @ApiOperation(value="加入购物车", notes="")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/add")
    @ResponseBody
    public ResultMap add(@RequestHeader String token,
                         @RequestParam(value = "userid") Integer userId,
                         @RequestParam(value = "productid") Integer productId,
                         @RequestParam(value = "count") Integer count){
        if (!GetAuthController.getUserAuth(token).equals("普通注册用户验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token)).token("").res("").data(null);
        }

        cartService.add(userId,productId,count);
//        System.out.println(product);
        return resultMap.success().code(200).message("加入购物车成功").token("").res("").data(null);
    }

    /**
     * 修改购物车商品数量
     */
    @ApiOperation(value="修改购物车商品数量", notes="")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/modifycartquantity")
    @ResponseBody
    public ResultMap modifyCartQuantity(@RequestHeader String token,
                         @RequestParam(value = "userid") Integer userId,
                         @RequestParam(value = "productid") Integer productId,
                         @RequestParam(value = "count") Integer count){
        if (!GetAuthController.getUserAuth(token).equals("普通注册用户验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token)).token("").res("").data(null);
        }

        if(count<1) {
            return resultMap.success().code(200).message("商品数量不能小于1").token("").res("").data(null);
        }
        cartService.modifyCartQuantity(userId,productId,count);
//        System.out.println(product);
        return resultMap.success().code(200).message("成功修改购物车商品数量").token("").res("").data(null);
    }

    /**
     * 购物车列表
     */
    @ApiOperation(value="获得购物车列表", notes="")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/get_cartlist")
    @ResponseBody
    public ResultMap get_productList (@RequestHeader String token, @RequestParam(value = "userid") Integer userId) {
        if (!GetAuthController.getUserAuth(token).equals("普通注册用户验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token)).token("").res("").data(null);
        }
        List<Map<String, String>> cartProductList = new ArrayList<Map<String, String>>();
        List<Map<String, String>> cartList = cartService.getCartList(userId);
        for (Map cart : cartList) {
            Map<String, String> product = productService.getProduct(Integer.valueOf(cart.get("product_id").toString()));
            Map<String, String> cartall = new HashMap<String, String>();
            cartall.putAll(cart);
            cartall.putAll(product);

            cartProductList.add(cartall);
        }
        return resultMap.success().code(200).message("获得购物车列表成功").token("").res(cartProductList).data(null);
    }
}
